package processes

import (
	"bufio"
	"encoding/json"
	"fmt"
	"go_code/project01_basic/chapter15_qq/client/utils"
	"go_code/project01_basic/chapter15_qq/message"
	"io"
	"os"
)

type SmsProcess struct {
}

//发送群聊消息
func (s *SmsProcess) SendGroupMes() (err error) {
	fmt.Print("消息:")
	var content string
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	content = scanner.Text()
	//1.创建消息
	var mes message.Message
	mes.Type = message.SmsMesType

	var smsMes message.SmsMes
	smsMes.Content = content
	smsMes.UserName = CurUser.User.UserName
	smsMes.UserId = CurUser.User.UserId

	//2.序列化消息
	data, err := json.Marshal(smsMes)
	if err != nil {
		fmt.Println("json.Marshal err=", err)
		return
	}

	mes.Data = string(data)

	//3.传递消息
	tf := &utils.Transfer{
		Conn: CurUser.Conn,
	}
	err = tf.Delivery(&mes)
	if err != nil {
		fmt.Println("com.Delivery err=", err)
		return
	}
	return
}

//发送私聊信息
func (s *SmsProcess) SendPrivateMes(id int) (err error) {
	fmt.Print("消息:")
	var content string
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	content = scanner.Text()
	if content == "bye退出" {
		return io.EOF
	}
	//1.创建消息
	var mes message.Message
	mes.Type = message.SmsPrivateMesType

	var smsPrivateMes message.SmsPrivateMes
	smsPrivateMes.Content = content
	smsPrivateMes.UserName = CurUser.User.UserName
	smsPrivateMes.UserId = CurUser.User.UserId
	smsPrivateMes.DstUser = id

	//2.序列化消息
	data, err := json.Marshal(smsPrivateMes)
	if err != nil {
		fmt.Println("json.Marshal err=", err)
		return
	}

	mes.Data = string(data)

	//3.传递消息
	tf := &utils.Transfer{
		Conn: CurUser.Conn,
	}
	err = tf.Delivery(&mes)
	if err != nil {
		fmt.Println("com.Delivery err=", err)
		return
	}
	return
}
